from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from microerp.apps.base.models import Users
from microerp.apps.purchaseorder.models import *
from microerp.apps.salesorder.models import *


class TransactionTable(models.Model):
    """
    transaction record include below fields:
    date time when the transaction happened
    transation type: purchase fee, sell fee, express fee
    income: the financial gain
    expenditure: money paid out
    memo: note other usefull information
    balance: net amount = income - cost(expenditure + express fee)
    """
    TRANS_TYPE = (('PO', 'Purchase Order'),
                  ('SO', 'Sales Order'),
                  ('EF', 'Express Fee'),)

    CURRENCY = (('USD', 'US Dollar'),
                ('CNY', 'China YUAN'),
                ('EUR', 'Euro'),)
    user = models.ForeignKey(Users, on_delete=None)
    trans_datatime = models.DateTimeField(
        _('transaction date'), default=timezone.now)
    trans_type = models.CharField(_('type'), max_length=50, choices=TRANS_TYPE)
    expenditure = models.DecimalField(
        _('expenditure'), max_digits=10, decimal_places=2)
    income = models.DecimalField(_('income'), max_digits=10, decimal_places=2)
    currency = models.CharField(max_length=20, choices=CURRENCY)
    memo = models.CharField(_('notes'), max_length=100)
    trans_balance = models.DecimalField(
        _('balance amount'), max_digits=10, decimal_places=2)
    salesorder = models.ForeignKey(SalesOrder, on_delete=None)

    class Meta:
        db_table = 'trans_table'
        verbose_name = _('Transaction Record')
        verbose_name_plural = _('Transaction Records')


class TransactionLine(models.Model):
    """
    detailed information of transaction
    """
    ACCOUNT_TYPE = (('WEIXIN', 'Weixin'),
                    ('ALIPAY', 'Alipay'),
                    ('BANK', 'Bank Account'),
                    ('BANK', 'Bank Account'),
                    ('CASH', 'CASH ON HAND'),)
    trans_table = models.ForeignKey(TransactionTable, on_delete=models.CASCADE)
    account_type = models.CharField(max_length=50, choices=ACCOUNT_TYPE)
    account = models.CharField(max_length=60)
    description = models.CharField(max_length=200)
    debit = models.DecimalField(max_digits=10, decimal_places=2)
    credit = models.DecimalField(max_digits=10, decimal_places=2)
    offset_accont_type = models.CharField(max_length=50, choices=ACCOUNT_TYPE)
    offset_account = models.CharField(max_length=60)

    class Meta:
        db_table = 'trans_line'
